import request from "@/utils/request";



/**
 * @description: 用户登录
 * @return {*}
 */
interface LoginData {
  studentId: string;
  password: string;
}

export async function LoginAPI(data: LoginData) {
  return request.post("/users/login", data);
}

/**
 * @description: 用户注销
 * @return {*}
 */
export async function LogoutAPI() {
  return request.post("/users/logout");
}

/**
 * @description: 获取指定用户信息
 */
export async function getUserInfoAPI(id: string) {
  return request.get(`/users/${id}`);
}

/**
 * @description: 获取用户列表
 */
export async function getUserAllListAPI(params: any) {
  return request.get("/users", { params });
}

/**
 * @description: 单一用户注册
 */
interface RegisterData {
  studentId: string;
  name: string;
  role: string;
  gender: string;
  phone: string;
  email: string;
  departmentId: string;
}

export const registerUserAPI = (data: RegisterData) => {
  return request({
    url: '/users/register',
    method: 'POST',
    data,
  });
};

/**
 * @description: 批量导入用户
 */
export const importUsersAPI = (file: File) => {
  const formData = new FormData();
  // 确保文件名包含扩展名
  const fileName = file.name || 'user_import.xlsx';
  formData.append('file', file, fileName);
  return request.post('/users/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    // 添加以下配置以确保正确处理FormData
    transformRequest: [function (data) {
      return data; // 不要转换FormData
    }],
  });
};


/**
 * @description: 删除用户
 */
export const deleteUserAPI = (id: string) => {
  return request.delete(`/users/${id}`);
};

/**
 * @description: 编辑用户信息
 */
export const updateUserAPI = (id: string, data: Partial<RegisterData>) => {
  return request.put(`/users/${id}`, data);
};


